<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>待办任务列表</title>
  <style>
    #app {
      width: 800px;
      margin: 0 auto;
      padding: 10px;
      border: 1px solid #ccc;
    }

   .content {
      width: 700px;
      margin: 0 auto;
    }

   .title {
      text-align: center;
    }

   .btn {
      height: 40px;
      width: 100px;
      margin-left: 10px;
      padding: 5px 10px;
      border: 1px solid #ccc;
      border-radius: 5px;
      background-color: #fff;
      cursor: pointer;
    }

   .all-count {
      background-color: #1363e3;
      color: #fff;
      padding: 10px 10px;
      border-radius: 5px;
    }

   .unfinish-count {
      background-color: #f4ad07;
      color: #fff;
      padding: 10px 10px;
      border-radius: 5px;
    }

   .task-input {
      width: 430px;
      height: 30px;
      padding: 5px 10px;
      border: 1px solid #ccc;
      border-radius: 5px;
      background-color: #fff;
      font-size: 16px;
    }

   .task-list {
      border-radius: 5px;
      list-style: none;
      padding: 0px;
    }

   .task-list li {
      padding: 5px;
      border: 1px solid #ccc;
      margin-top: 3px;
      border-radius: 5px;
      height: 40px;
      line-height: 40px;
    }

   .ckbox {
      width: 16px;
      height: 16px;
      cursor: pointer;
    }

   .done {
      text-decoration: line-through;
      color: #fff;
      background-color: green;
    }

   .normal {
      color: #000;
      background-color: #fff;
    }
  </style>
</head>

<body>
  <div id="app">
    <h1 class="title">我的待办任务</h1>
    <div class="content">
      <p>
        <span class="all-count">任务总数：{{ tasks.length }} 个</span>
        <span class="unfinish-count">未完成：{{ getUnfinishedCount() }} 个</span>
      </p>
      <p>
        <input v-model="newTask" class="task-input" placeholder="请输入新待办任务" type="text" />
        <button @click="addTask" class="btn">添加任务</button>
        <button @click="clearCompleted" class="btn">清除已完成</button>
      </p>
      <p>
        <h3>任务列表</h3>
      </p>
      <ul class="task-list">
        <li v-for="(task, index) in tasks" :key="index" :class="{ done: task.completed, normal:!task.completed }">
          <input type="checkbox" class="ckbox" v-model="task.completed" @change="updateUnfinishedCount">
          <input type="text" v-if="editingIndex === index" v-model="task.text" class="task-input" @blur="saveTask(index)" />
          <span v-else @click="editTask(index)">{{ task.text }}</span>
        </li>
      </ul>
    </div>

  </div>

  <script src="https://cdn.jsdelivr.net/npm/vue@2"></script>
  <script>
    new Vue({
      el: '#app',
      data: {
        tasks: [
          { text: '学习Vue', completed: false },
          { text: '学习React', completed: true },
          { text: '学习Angular', completed: false }
        ],
        newTask: '',
        editingIndex: null
      },
      methods: {
        addTask() {
          if (this.newTask.trim()!== '') {
            this.tasks.push({ text: this.newTask, completed: false });
            this.newTask = '';
          }
        },
        editTask(index) {
          this.editingIndex = index;
        },
        saveTask(index) {
          this.editingIndex = null;
        },
        updateUnfinishedCount() {
          this.getUnfinishedCount();
        },
        getUnfinishedCount() {
          return this.tasks.filter(task =>!task.completed).length;
        },
        clearCompleted() {
          this.tasks = this.tasks.filter(task =>!task.completed);
        }
      }
    });
  </script>
</body>

</html>